package com.hxz.admin.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hxz.admin.service.AdminArticleModifyService;
import com.hxz.entity.Article;

import io.swagger.annotations.Api;
/**
 * 
 * @author hxz
 * @description TODO
 * @data 2019年12月5日 上午9:30:58
 */
@Controller
@Api(description = "后台管理系统文章发布controller")
@RequestMapping("/admin/article")
public class AdminArticleModifyController {
	private static final Logger logger = LoggerFactory.getLogger(AdminArticleModifyController.class);

	@Autowired
	private AdminArticleModifyService modifyService;

	/**
	 * 文章列表页面
	 *
	 * @param page
	 * @param limit
	 * @param request
	 * @return
	 */
	@GetMapping(value = "")
	public String index(@RequestParam(value = "page", defaultValue = "1") int page,
			@RequestParam(value = "limit", defaultValue = "8") int limit, HttpServletRequest request) {
		PageHelper.startPage(page, limit);
		List<Article> list = modifyService.getArticleList();
		PageInfo<Article> pageInfo = new PageInfo<>(list);
		request.setAttribute("pageInfo", pageInfo);
		return "admin/article_list";
	}

	/**
	 * 文章发表页面
	 *
	 * @param request
	 * @return
	 */
	/*
	 * @GetMapping(value = "/publish") public String
	 * newArticle(HttpServletRequest request) { List<MetaVo> categories =
	 * metaService.getMetas(Types.CATEGORY.getType());
	 * request.setAttribute("categories", categories);
	 * request.setAttribute(Types.ATTACH_URL.getType(),
	 * Commons.site_option(Types.ATTACH_URL.getType())); return
	 * "admin/article_edit"; }
	 */

	/**
	 * 文章编辑页面
	 *
	 * @param cid
	 * @param request
	 * @return
	 */

	@GetMapping(value = "/modify/{id}")
	public String editArticle(@PathVariable String id, HttpServletRequest request) {
		Article article = modifyService.getContents(id);
		request.setAttribute("article", article);
		request.setAttribute("active", "article");
		return "admin/article_edit";
	}

	/**
	 * 文章发表 post
	 *
	 * @param contents
	 * @param request
	 * @return
	 *//*
		 * @PostMapping(value = "/publish")
		 * 
		 * @ResponseBody
		 * 
		 * @Transactional(rollbackFor = TipException.class) public
		 * RestResponseBo publishArticle(ContentVo contents, HttpServletRequest
		 * request) { UserVo users = this.user(request);
		 * contents.setAuthorId(users.getUid());
		 * contents.setType(Types.ARTICLE.getType()); if
		 * (StringUtils.isBlank(contents.getCategories())) {
		 * contents.setCategories("默认分类"); } try {
		 * contentService.publish(contents); } catch (Exception e) { String msg
		 * = "文章发布失败"; return ExceptionHelper.handlerException(logger, msg, e);
		 * } return RestResponseBo.ok(); }
		 * 
		 * /** 文章更新 post
		 *
		 * @param contents
		 * 
		 * @param request
		 * 
		 * @return
		 */
	@PostMapping(value = "/modify/save")
	@ResponseBody
	public Map<String, String> modifyArticle(Article article, HttpServletRequest request) {
		Map<String, String> result = new HashMap<String, String>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		article.setDate(sdf.format(new Date()));
		try {
			modifyService.updateArticle(article);
		} catch (Exception e) {
			result.put("msg", "fail");
			return result;
		}
		result.put("msg", "success");
		return result;
	}

	/**
	 * 删除文章 post
	 *
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public Map<String, String> deleteArticleById(@RequestParam(value = "id", required = true) Integer id) {
		Map<String, String> result = new HashMap<String, String>();
		Integer statusCode = modifyService.deleteArticleById(id);
		if (statusCode>0) {
			result.put("msg", "success");
		}else {
			result.put("msg", "fail");
		}
		return result;
	}

}
